﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace View
{

    public partial class LoginForm : Form
    {

        private LoginEventArgs _properties;

        public LoginForm()
        {
            InitializeComponent();
        }

        public void SafeClose()
        {
            _properties = null;
            Visible = false;
        }

        public void ShowDialog(LoginEventArgs fields)
        {

            _properties = fields;
            Fields2Controls();
            Show();
            _properties.Username = "admin"; // remove from final
            _properties.Password = "admin"; // remove from final
            OnSubmit();
        }

        /*
         * Private methods
         */

        private void Controls2Fields()
        {
            _properties.Username = textBoxUsername.Text;
            _properties.Password = textBoxPassword.Text;
        }

        private void Fields2Controls()
        {
            textBoxUsername.Text = _properties.Username;
            textBoxPassword.Text = _properties.Password;
        }

        /*
         * Internal event handlers
         */

        private void buttonLogin_Click(object sender, EventArgs e)
        {
            Controls2Fields();
            OnSubmit();
        }

        private void buttonExit_Click(object sender, EventArgs e)
        {
            OnExit();
        }

        private void LoginForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            Hide();
        }

        /*
         * Events
         */

        public event EventHandler<LoginEventArgs> Submit;

        private void OnSubmit()
        {
            if (Submit != null) { Submit(this, _properties); }
        }

        public event EventHandler Exit;

        private void OnExit()
        {
            if (Exit != null) { Exit(this, null); }
        }

        private void textBoxPassword_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Controls2Fields();
                OnSubmit();
            }
        }

    }
}
